﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;

namespace BesAsm.Framework.Reporting
{
  public partial class MessengerUserControl : UserControl
  {
    int _messageTally = 0;
    List<ReportableMessage> _messageList = new List<ReportableMessage>();
    private List<ReportableMessageType> _types;
    public event MessageReportedEventHandler ErrorMessageReported;

    public MessengerUserControl()
    {
      InitializeComponent();           
    }

    private void InitializeMessages()
    {
      lstViewMessages.Items.Clear();

      while (Messenger.MessageCount > 0)
      {
        ReportableMessage message = Messenger.GetMessage();
        if (_types.Contains(message.MessageType))
          DisplayMessage(message);

        if (message.MessageType == ReportableMessageType.Error && ErrorMessageReported != null)
          ErrorMessageReported(this, new MessageReportedArgs(message));

      }

      Messenger.MessageQueued += new MessageReportedEventHandler(ReportMessageHandler);
    }

    private void DisplayMessage(ReportableMessage reportableMessage)
    {
      _messageList.Add(reportableMessage);
      _messageTally++;
      Infragistics.Win.UltraWinListView.UltraListViewItem messageItem =
        new Infragistics.Win.UltraWinListView.UltraListViewItem(_messageTally.ToString());
      messageItem.Value = _messageTally.ToString();

      lstViewMessages.Items.Add(messageItem);

      messageItem.Appearance.Image = Convert.ToInt32(reportableMessage.MessageType);
      messageItem.SubItems["Time"].Value = reportableMessage.CreationTime.ToString(@"HH:mm:ss.fff");
      messageItem.SubItems["Message"].Value = reportableMessage.Description;
      messageItem.BringIntoView();

      foreach (Infragistics.Win.UltraWinListView.UltraListViewSubItemColumn column in lstViewMessages.SubItemColumns)
        column.PerformAutoResize(Infragistics.Win.UltraWinListView.ColumnAutoSizeMode.AllItemsAndHeader);
    }

    private void ReportMessageHandler(object sender, MessageReportedArgs e)
    {
      if (_types.Contains(e.Message.MessageType))
        DisplayMessage(e.Message);

      if (e.Message.MessageType == ReportableMessageType.Error && ErrorMessageReported != null)
          ErrorMessageReported(this, new MessageReportedArgs(e.Message));
    }

    private bool _errorsOnly = false;

    public bool ShowErrorMessagesOnly
    {
      get
      {
        return _errorsOnly;
      }
      set
      {
        _errorsOnly = value;
        List<ReportableMessageType> types = new List<ReportableMessageType>();

        if (_errorsOnly)
          types.Add(ReportableMessageType.Error);
        else
          foreach (var type in Enum.GetValues(typeof(ReportableMessageType)))
            types.Add((ReportableMessageType)type);
        _types = types;
      }
    }

    private void MessengerUserControl_Load(object sender, EventArgs e)
    {
      InitializeMessages();
    }
  }
}
